package com.ftg.learn.working;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

/**
 * @author kangjx
 * @createTime 2022/9/21
 */
public class LearnRabbitPublish {


    public static void main(String[] args) throws IOException {
        ConnectionFactory cf = new ConnectionFactory();
        cf.setHost("127.0.0.1");
        cf.setPort(5673);
        cf.setVirtualHost("dev");
        cf.setUsername("kangjx");
        cf.setPassword("kangjx");
        try (
                Connection conn = cf.newConnection();
                Channel c = conn.createChannel();
        ) {
            c.queueDeclare("hello1", true, false, false, null);
            c.basicQos(1);
            for( int i = 0 ;i < 10;i++){
                String message = "d"+i+"...";
                c.basicPublish("", "hello1", MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes(StandardCharsets.UTF_8));
            }

            System.out.println("添加成功");
        } catch (IOException | TimeoutException e) {
            e.printStackTrace();
        }
    }

}
